/**
 * 购货订单js脚本 
 */
corpApp.controller('controller',['$scope', 'upserve', 'ngDialog', function($scope, upserve, ngDialog){
	//excel名称
	$scope.fileName ='计划下发列表';
	//状态数
	$scope.statusData01 = 0;
	$scope.statusData02 = 0;
	$scope.statusData03 = 0;
	$scope.statusData04 = 0;
	$scope.statusData05 = 0;
	$scope.statusData06 = 0;
	$scope.statusData07 = 0;
	$scope.statusData08 = 0;
	//时间控件
	$scope.startOpend = false;
	$scope.endOpend = false;
	$scope.tipShow = false;
	var d = new Date();
	d.setDate(d.getDate()-7);
	$scope.startDate = d.format('yyyy-MM-dd');
	$scope.openStartDatePicker = function($event){
		 $event.preventDefault();
         $event.stopPropagation();
         $scope.startOpend =  !$scope.startOpend;
	}
	
	$scope.openEndDatePicker = function($event){
		 $event.preventDefault();
         $event.stopPropagation();
         $scope.endOpend = !$scope.endOpend;
	}
	
	$scope.grid = {};
	//搜索条件
	$scope.condition = {
			OrderStatusCode:'',
			BillDateStart:'',
			BillDateEnd:'',
			VendorName:'',
			VendorCode:'',
			PersonCode:'',
			VendorModeCode:'',
			OrderCode:''
	};
	//订单状态
	$scope.condition.OrderStatusCode = angular.element('#txtStatusCode').val();
	
	/**
	 * 主表列
	 */
	$scope.columns = [
	    {headerTemplate: '', title:'',template:'<input type="checkbox" id="check-all" ng-model="dataItem.check_row" />',  width:20, sortable:false, field:'check_row', attributes:{style:'text-align:center'}},
		{headerAttributes:{style:"text-align:center"},field:'',title:'操作',width:100, hidden:false, attributes:{style:"text-align:center"}, template:function(e){
			var template = '<a style="color:#f89d3b" ng-click="send()" >下发 </a>';
				template +="&nbsp;|&nbsp;&nbsp;<a href='' style='color:#f89d3b' ng-click='cancel()'>取消 </a>";
			return template;
			},
			headerAttributes:{style:"text-align:center"}
		},
		{headerAttributes:{style:"text-align:center"},field:'PlanName',title:'计划名称', width:window.longNameWidth, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'DocNumber',title:'文号', width:window.longNameWidth, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'FileName',title:'文件', width:window.remarkWidth, hidden:false}
	];
	/**
	 * 合计项
	 */
	$scope.aggregate = [
	   {field:'InTaxMoney', aggregate:'sum'},
	  
    ];
	
	 $scope.send = function(){
		 upserve.confirm('确认要下发此条数据吗？', function(result){
		 });
	 }
	 
	 $scope.cancel = function(){
		 upserve.confirm('确认要取消此条数据吗？', function(result){
		 });
	 }
	 
	/**
	 * 列表参数设置
	 */
	$scope.options = {
			excelExport: function(e) {
			    var sheet = e.workbook.sheets[0];
			    var reg = new RegExp('>(.*)<');
			    var length = sheet.rows.length;
			    angular.forEach(sheet.rows, function(row){
			    	if(row.type==='group-footer'||row.type==='footer'){
			    		angular.forEach(row.cells, function(cell){
			    			var matchs = reg.exec(cell.value);
			    			if(matchs!=null){
			    				cell.value = matchs[1];
			    				if(cell.value=='合计'||cell.value=='小计'){
			    					cell.hAlign = 'left';
			    				}else{
			    					cell.hAlign = 'right';
			    				}
			    			}
			    		});
			    	}
			    });
			},
			excel: {
	            allPages: true,
	            fileName:$scope.fileName + '.xlsx'
	        },
	        dataBound: function(e) {
            	var grid = e.sender;
            	var gridData = grid.dataSource.view();
                for (var i = 0; i < gridData.length; i++) {
                	//用于修改记录的背景色
                    if (gridData[i].ClosedownFlag == '1') {
                        grid.table.find("tr[data-uid='" + gridData[i].uid + "']").css("color", 'green');
                    }
                    if (gridData[i].ClosedownFlag == '2') {
                        grid.table.find("tr[data-uid='" + gridData[i].uid + "']").css("color", 'blue');
                    }
                }
            },
			resizable:true,
			sortable: true,
		    selectable:true,
		    height: 550,
		    pageable:true,
		    columns:$scope.columns
		   
	};
	/**
	 * 主表数据参数设置
	 */
	$scope.dataSource = new kendo.data.DataSource({
	     data:[],
		 page: 1,
		 pageSize: 20,
		 aggregate:$scope.aggregate,
		 schema:{
			 model:{
				 fields:{
					 DeliveryDate:{
						 type:'date'
					 }
				 }
			 }
		 }
	});
	/**
	 * 改变搜索状态,刷新数据
	 */
	$scope.changeQueryStatus = function(status){
		$scope.condition.OrderStatusCode = status;
		$scope.loadData();
	}
	/**
	 * 加载主表数据
	 */
	$scope.loadData = function(){
		//kendo.ui.progress(angular.element("#loading"), true);
		
		data=[{
			"PlanName":"第一批",
			"DocNumber":"晋文【2015】001号",
			"FileName":"晋文【2015】001号.pdf"
		},
		{
			"PlanName":"第二批",
			"DocNumber":"晋文【2015】002号",
			"FileName":"晋文【2015】002号.pdf"
		},
		{
			"PlanName":"第三批",
			"DocNumber":"晋文【2015】003号",
			"FileName":"晋文【2015】003号.pdf"
		},
		]
		$scope.dataSource.data(data);
	}
	$scope.loadData();
	

    //获取状态的数量
    $scope.getStatusData = function(){
    	
    	$scope.statusData03=3
    	$scope.statusData02 =1
//		
	};
	 $scope.getStatusData();
	 
	 $scope.exportToExcel = function(){
		 	if($scope.grid.dataSource.data().length==0){
	    		upserve.tip('当前没有需要导出的数据', showTime);
	    		return ;
	    	}
	    	$scope.grid.saveAsExcel();
	    }
	 $scope.deleteBatch=function(){
		 upserve.confirm('确认要删除数据吗？', function(result){
		 });
	 }
}]);
